import Vue from "vue";
import Vuex from "vuex";
import $request from "@/config/request.js";
import $api from "@/config/api.js";
Vue.use(Vuex);
const store = new Vuex.Store({
	state: {
		userInfo: null,
		projects: [],
		token: null
	},
	mutations: {
		saveToken(state, token) {
			state.token = token;
		},
		saveUserInfo(state, userInfo) {
			state.userInfo = userInfo;
		},
		saveProjects(state, data) {
			state.projects = data;
		},
		resetAll(state) {
			state.token = null;
			state.userInfo = null;
		}
	},
	actions: {
		async getProjects(state) {
			const res = await $request.request({
				url: $api.getProject,
				method: "GET",
				isNotLoading: true,
			});
			state.commit("saveProjects", res);
		},
	},
	getters: {
		getIslogin(state) {
			return state.token ? true : false
		},
		getUserInfoName(state) {
			return state?.userInfo?.name || ''
		},
		getUserInfoUserName(state) {
			return state?.userInfo?.username || ''
		},
		getUserType(state) {
			return state?.userInfo?.role || ''
		},
	}
});
export default store;
